package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.ReportBean;
import com.zerowidth.db.utils.IOUtils;
import com.zerowidth.db.utils.TimeUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReportDB {

    public static int count(String start_time, String end_time) {
        String sql = String.format("SELECT COUNT(*) FROM report WHERE create_time >= '%s' " +
                " AND create_time <= '%s'", start_time, end_time);

        return DBHelper.queryCount(sql);
    }

    public static boolean insert(String phone, String userId, String content) {
        String time = TimeUtils.convert(System.currentTimeMillis(), TimeUtils.TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);

        String sql = String.format("INSERT INTO report(phone, user_id, content, create_time) " +
                        "VALUES('%s', '%s', '%s', '%s')",
                phone, userId, content, time);

        return DBHelper.executeInsert(sql) > 0;
    }

    public static boolean insert(String phone, String userId, String content, String reply_content) {
        String time = TimeUtils.convert(System.currentTimeMillis(), TimeUtils.TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);

        String sql = String.format("INSERT INTO report(phone, user_id, content, create_time, reply_content) " +
                        "VALUES('%s', '%s', '%s', '%s', '%s')",
                phone, userId, content, time, reply_content);

        return DBHelper.executeInsert(sql) > 0;
    }

    public static List<ReportBean> getNoticeList(String userId) {

        String sql = String.format("SELECT * FROM report where user_id = '" + userId + "'");

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            List<ReportBean> vipEnumBeanList = new ArrayList<>();
            while (rs.next()) {
                ReportBean bean = new ReportBean();
                bean.id = rs.getInt("id");
                bean.phone = rs.getString("phone");
                bean.content = rs.getString("content");
                bean.reply = rs.getString("reply_content");
                bean.user_id = rs.getString("user_id");
                bean.create_time = rs.getString("create_time");

                vipEnumBeanList.add(bean);
            }

            return vipEnumBeanList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }


}
